package com.clothing.interceptor;

import com.clothing.CommonAttributes;
import com.clothing.entity.User;
import com.clothing.service.UserService;
import com.clothing.utils.Utils;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @ClassName: UserInterceptor
 * @Description: TODO(拦截器)
 * @author Charles.Tao
 * @date 2017年9月6日 下午4:11:55
 * @version 1.0
 */
public class UserInterceptor extends BaseInterceptor
{
    @Autowired
    private CacheManager cacheManager;
    
    @Autowired
    private UserService userService;
    
    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception
    {
//        String thirdSessionId = request.getHeader(CommonAttributes.THIRD_SESSION_ID);
//
//        if (Utils.isEmpty(thirdSessionId))
//        {
//            return forbid(response, CommonAttributes.TOKEN_ERROR_MESSAGE);
//        }
//
//        Ehcache cache = cacheManager.getEhcache(User.SESSION_CACHE_NAME);
//        Element element = cache.get(thirdSessionId);
//
//        if (null == element)
//        {
//            return forbid(response, CommonAttributes.TOKEN_ERROR_MESSAGE);
//        }
//
//        String sessionValue = (String) element.getObjectValue();
//
//        if (Utils.isEmpty(sessionValue))
//        {
//            return forbid(response, CommonAttributes.TOKEN_ERROR_MESSAGE);
//        }
//
//        String[] strs = sessionValue.split(":");
//
//        if (strs.length != 2)
//        {
//            return forbid(response, CommonAttributes.TOKEN_ERROR_MESSAGE);
//        }
//
//        User user = userService.findByOpenid(strs[0]);
        User user = userService.find(13L);
        
        if (null == user)
        {
            return forbid(response, CommonAttributes.TOKEN_ERROR_MESSAGE);
        }
        
        request.setAttribute("user", user);
        return true;
    }
}
